package top.abilities.util.util;

import java.util.*;

/**
 * @Author ciro
 * @Date 2017/11/13 15:02
 */
public class ArrayUtil {
    /**
     * @Description: collection true is null,false is not null
     * @Param: [collection]
     * @Return boolean
     * @Author ciro
     * @Date 2017/11/13 15:14
     */
    public static boolean isEmpty(Collection<?> collection){
        return collection==null||collection.isEmpty();
    }
    /**
     * @Description: map true is null,false is not null
     * @Param: [map]
     * @Return boolean
     * @Author ciro
     * @Date 2017/11/13 15:16
     */
    public static boolean isEmpty(Map<?,?> map){
        return map==null || map.isEmpty();
    }
    /**
     * @Description: Arrays true is null,false is not null
     * @Param: [object]
     * @Return boolean
     * @Author ciro
     * @Date 2017/11/13 15:19
     */
    public static boolean isEmpty(Object[] object){
        return object==null || object.length==0;
    }

    /**
     * @Description: 切分list
     * @Param: [list, oneSize]
     * @Return java.util.List<java.util.List<T>>
     * @Author ciro
     * @Date 2017/11/13 15:22
     */
    public static<T> List<List<T>> splitList(List<T> list, int oneSize){
        int l = list.size();
        int s = oneSize;
        int n = l % s == 0 ? l / s : l / s + 1;
        List ll = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            List il = new ArrayList<>();
            int end = (i + 1) * s > l ? l : (i + 1) * s;
            for (int j = s * i; j < end; j++) {
                il.add(list.get(j));
            }
            ll.add(il);
        }
        return ll;
    }

    /**
     * java统计List集合中每个元素出现的次数
     * 例如frequencyOfListElements(["111","111","222"])
     *  ->
     * 则返回Map {"111"=2,"222"=1}
     * @param items
     * @return  Map<String,Integer>
     * @author wuqx
     */
    public static Map<String,Integer> frequencyOfListElements( List<String> items ) {
        if (items == null || items.size() == 0) return null;
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String temp : items) {
            Integer count = map.get(temp);
            map.put(temp, (count == null) ? 1 : count + 1);
        }
        return map;
    }
}
